from flask import jsonify
# 统一返回接口
class APIUtils:

    @staticmethod
    def success_response(data=None, message="Success", status_code=200):
        """成功响应封装"""
        response = {
            'code': '200',
            'message': message,
            'data': data
        }
        return jsonify(response), status_code

    @staticmethod
    def error_response(message="Error", status_code=400):
        """错误响应封装"""
        response = {
            'code': '500',
            'message': message
        }
        return jsonify(response), status_code

    @staticmethod
    def validate_json(request_json, required_fields):
        """验证 JSON 请求体中是否包含所需字段"""
        missing_fields = [field for field in required_fields if field not in request_json]
        if missing_fields:
            return False, f'缺失字段: {", ".join(missing_fields)}'
        return True, ""

    @staticmethod
    def paginate(query, page, per_page):
        """分页处理"""
        total = query.count()
        items = query.offset((page - 1) * per_page).limit(per_page).all()
        return {
            'total': total,
            'items': items
        }
